package tournament.volleyball;

import java.util.ArrayList;

public class Team {
	private static int nextId = 0;
	private int id;
	
	private String teamName;
	
	private transient ArrayList<PoolRivalry> sets;
	
	private Team() {
	}
	
	Team(String name) {
		this.id = nextId;
		Team.nextId += 1;
		
		this.sets = new ArrayList<PoolRivalry>();
		this.teamName = name;
	}
	
	String getName() {
		return teamName;
	}
	
	void setName(String name) {
		this.teamName = name;
	}

	int getWins() {
		int wins = 0;
		for (int i=0; i<sets.size(); i++) {
			wins += sets.get(i).getWins(this);
		}
		return wins;
	}
	
	int getLosses() {
		int losses = 0;
		for (int i=0; i<sets.size(); i++) {
			losses += sets.get(i).getLosses(this);
		}
		return losses;
	}
	
	// Unfortunately, the only way to properly do this is to cast the Rivalry Object
	// into a PoolRivalry Object so that we can use the other necessary functions.	
	void updateTeamWithSetGames(ArrayList<Rivalry> rivalries) {
		for(int i=0;i<rivalries.size();i++){
			if(rivalries.get(i).hasTeam(this)){
				sets.add((PoolRivalry) rivalries.get(i));
			}
		}
		if(sets.isEmpty()){
			System.out.println("There are no games with this team according to"
					+ " the method division.getGame().hasTeam(this)");
		}
	}

	public int getSpread() {
		return getTotalPointsScored() - getTotalPointsAllowed();
	}
	
	public int getTotalPointsScored() {
		int pointsScored = 0;
		
		for (int i=0; i<sets.size(); i++) {
			pointsScored += sets.get(i).getPointsScored(this);
		}
		
		return pointsScored;
	}
	
	public int getTotalPointsAllowed() {
		int pointsAllowed = 0;
		
		for (int i=0; i<sets.size(); i++) {
			pointsAllowed += sets.get(i).getPointsAllowed(this);
		}
		
		return pointsAllowed;
	}
	
	public int getId() {
		return id;
	}

}
